<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Ad_Models_Mysql_BannerPageAssoc extends Modules_Ad_Models_BannerPageAssoc
{	
	protected $_table = 'ad_page_assoc';
	
	public function add($bannerPageAssoc)
	{
		return $this->_conn->insert($bannerPageAssoc, $this->_conn->_tablePrefix . $this->_table);
	}
	
	public function deleteByBannerId($bannerId)
	{
		$sql = 'DELETE FROM ' . $this->_conn->_tablePrefix . $this->_table . ' 
							WHERE banner_id = ' . $this->_conn->escape($bannerId);
		$rs = $this->_conn->query($sql);
		$affectRow = $this->_conn->affectedRows();
		$this->_conn->freeResult($rs);
		return $affectRow;
	}
	
	public function getBannerPageAssoc($bannerId)
	{
		$sql  = sprintf("SELECT pa.*, z.code FROM " . $this->_conn->_tablePrefix . $this->_table . " AS pa
						INNER JOIN " . $this->_conn->_tablePrefix . "ad_zone AS z
							ON pa.zone_id = z.zone_id
						WHERE pa.banner_id = '%s'",
						$this->_conn->escape($bannerId));
		$rs = $this->_conn->query($sql);
		$rows = array();
		while ($row = $this->_conn->fetchAll($rs)) {
			$rows[] = $row;
		}
		/**
		 * Free result 
		 */
		$this->_conn->freeResult($rs);
		return $rows;	
	}
}